package com.su02.multi.chainrule;

import java.util.Map;

public class Neg extends Function {
    private final Function f;

    public Neg(Function f) {
        this.f = f;
        this.setBs(f.getBs());
    }

    @Override
    public Function doBackward(int id) {
        Function ans;
        if (isNotRelated(id)) {
            ans = new Constant(0);
        } else {
            ans = f.backward(id).neg();
        }
        return ans;
    }

    @Override
    public double forward(Map<Integer, Double> vector) {
        return - f.forward(vector);
    }
}
